import {
  getBlog,
  getComment,
  getPersonalBlog,
  getPersonalBlogInfo,
  like,
  makeComment,
  publishBlog,
  revokeLike,
  searchBlog,
  upLoadBlogImage,
  revokeBlog
} from '@/api/blog'
import type { BlogDetail, BlogParams, BlogPreview, Comment, PersonalBlogInfo } from '@/models/blog'

export async function GetPersonalBlogInfo(id?: number): Promise<PersonalBlogInfo> {
  const res = (await getPersonalBlogInfo(id)) as any
  const data = res.data
  const result: PersonalBlogInfo = {
    specialty: data.specialty,
    signature: data.signature,
    totalBrowse: data.browse,
    totalLike: data.likes,
    blogNumber: data.blogNumber,
    profile: data.profile
  }
  return result
}

export async function GetPersonalBlog(id?: number): Promise<BlogPreview[]> {
  const res = (await getPersonalBlog(id)) as any
  const data = res.data as any[]
  const result: BlogPreview[] = []
  data.forEach((val) => {
    const prev: BlogPreview = {
      id: val.id,
      userId: val.userId,
      title: val.title,
      img: val.image,
      abstract: val.digest,
      like: val.like,
      browse: val.browse,
      comment: val.comment,
      author: val.author,
      selfPublish: val.selfPublish
    }
    result.push(prev)
  })
  return result
}

export async function SearchBlog(
  params?: BlogParams
): Promise<{ preview: BlogPreview[]; total: number }> {
  const res = (await searchBlog(params)) as any
  const data = res.data
  const blog = data.blog as any[]
  const result: BlogPreview[] = []
  blog.forEach((val) => {
    const prev: BlogPreview = {
      id: val.id,
      userId: val.userId,
      title: val.title,
      img: val.image,
      abstract: val.digest,
      like: val.like,
      browse: val.browse,
      comment: val.comment,
      author: val.author,
      selfPublish: val.selfPublish
    }
    result.push(prev)
  })
  return {
    preview: result,
    total: data.total
  }
}
export async function GetBlog(id: number): Promise<BlogDetail> {
  const res = (await getBlog(id)) as any
  const data = res.data
  const result: BlogDetail = {
    title: data.title,
    img: data.image,
    content: data.content,
    abstract: data.digest,
    like: data.like,
    browse: data.browse,
    comments: [],
    author: data.author,
    authorId: data.userId,
    selfPublish: data.selfPublish,
    alreadyLiked: data.alreadyLiked
  }
  const comments = data.comments as any[]
  comments.forEach((val) => {
    const c: Comment = {
      profile: val.profile,
      name: val.name,
      comment: val.comment,
      time: val.time
    }
    result.comments.push(c)
  })
  return result
}

export async function Like(id: number): Promise<void> {
  await like(id)
}

export async function RevokeLike(id: number): Promise<void> {
  await revokeLike(id)
}

export async function MakeComment(comment: string, id: number): Promise<void> {
  await makeComment(comment, id)
}
export async function GetComment(id: number): Promise<Comment[]> {
  const res = (await getComment(id)) as any
  const comments = res.data as any[]
  const result: Comment[] = []
  comments.forEach((val) => {
    const c: Comment = {
      profile: val.profile,
      name: val.name,
      comment: val.comment,
      time: val.time
    }
    result.push(c)
  })
  return result
}
export async function UpLoadBlogImage(img: File): Promise<string> {
  const res = (await upLoadBlogImage(img)) as any
  return res.data
}
export async function PublishBlog(
  title: string,
  content: string,
  abstract?: string,
  img?: File
): Promise<void> {
  await publishBlog(title, content, abstract, img)
}
export async function RevokeBlog(id: number) {
  await revokeBlog(id)
}
